package com.study.thirdpartyeventconsumer.多线程;

import java.util.concurrent.*;

/**
 * 1.线程如何命名？
 */
public class 多线程 {
    public static void main(String[] args) {
        //TODO:实现Runnable接口
        Thread thread = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName() + "---正在运行---" + i);
                try {
                    Thread.sleep(100L);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        //线程命名
        thread.setName("中国人民真高兴");
        thread.start();

        //TODO:继承Thread类，重写run方法
        MyThread myThread = new MyThread();
        myThread.start();

        //TODO:线程池创建线程（1）
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.submit(() -> {
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName() + "---正在运行---" + i);
                try {
                    Thread.sleep(100L);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        ExecutorService executorService = Executors.newFixedThreadPool(3);
        ExecutorService executorService1 = Executors.newCachedThreadPool();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5,
                10,
                5,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(),
                new ThreadPoolExecutor.AbortPolicy());


        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + "---正在运行---" + i);
        }
    }
}
